<?php
/**
 * 建立目录
 * @version 2018-1-31
 */
// 版本号
define('VERSION', '1.8.0');
// 程序名称
define('APPNAME', 'Mirrors目录生成器');
// 日志根目录
define('ROOTPATH', './');

// 引入头文件
include './pts_head.php';
// #JWTLite配置位置
// 页面访问验证配置
JWTLite::check_login();
list($loginid, $token, $loginexp) = array_values(JWTLite::$LOGIN_RESULT);

// 浏览器输入参数
$hy = input('hy');
$app = input('app');
$action = input('action');
if ($action) {
    if ($hy) {
        $path = ROOTPATH . 'hy_' . $hy;
        xsfn_mkdir($path);
        if ($app) {
            $path = $path . '/' . $app;
            xsfn_mkdir($path);
            // app子目录
            $appdirs = ['asset', 'screenshot'];
            foreach ($appdirs as $appdir) {
                xsfn_mkdir($path . '/' . $appdir . '/');
            }
            // app子文件
            $appfiles = ['intro.txt' => '<pre></pre>', 'git.txt' => '', 'tag.txt' => ''];
            $pathtmp = '';
            foreach ($appfiles as $fname => $fcontent) {
                $pathtmp = $path . '/' . $fname;
                if (!is_file($pathtmp)) {
                    file_put_contents($pathtmp, $fcontent);
                }
            }
        }
        redirect(true, '目录生成成功!', '?token=' . $token);
    }
}

function xsfn_mkdir($path) {
    if (!is_dir($path)) {
        try {
            mkdir($path);
        } catch (Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>build - taocode</title>
        <style type="text/css">
            body{font-size:12px; color:#333;}
            /*
            table {border:solid 1px #CCC; border-top:none;border-left:none;}
            th, td{border:solid 1px #CCC; border-bottom:none;border-right:none; padding:5px 10px;}
            tr:nth-child(odd) {background-color:#efefef;}
            */
            th, td {padding:3px 100px 3px 0; text-align: left;}
            a { text-decoration: none; color:#06C}
            .nav { margin:5px 0; color:#666;}
            .nav a { color:#666;}
            .nav a:hover { color:#333;}
            .nav a:before { content: ' » ' }
            .nav a:first-child:before {content: ''}
            .nav div { color:#CCC; border-bottom:solid 1px #CCC; margin-bottom:5px;}
        </style>
    </head>
    <body>
        <?php
        echo '<h1>', APPNAME, '</h1>';
        // 用户登录信息
        if (isset($loginid)) {
            echo '<span title="登录过期时间：', $loginexp, ' (超时前进行跳转操作能让登录会话自动延期,避免重登录操作~)">', $loginid, '</span>';
            echo ' <a href="?action=logout" onclick="return confirm(\'您确定要注销登录吗?\');">[注销登录]</a>';
            echo '<a href="?token=', $token, '" style="font-size:12px;">[刷新]</a>';
        }
        echo '<a href="index.php?token=', $token, '" style="font-size:12px;">[首页]</a>';
        ?>
        <form>
            <div style="margin: 5px 0;">
                行业代码：<input type="text" name="hy" placeholder="行业代码" required="required" /><br />
                应用代码：<input type="text" name="app" placeholder="应用代码" required="required" /><br />
            </div>
            <button type="submit">生成</button>
            <button type="reset">重置</button>
            <input type="hidden" name="action" value="build" />
            <input type="hidden" name="token" value="<?php echo $token ?>" />
        </form>
    </body>
</html>
